<template>
  <div class="common-layout" id='imgtxtrpt'>
    <el-main :loading="iloading">
      <el-row v-for='(item) in fields' :key="item.id">
        <el-col :span="24" v-if="item.typeFlag != '4'">
          <el-form-item :label="item.tempItemNa+'：'" label-width='100px'>
            <vxe-textarea v-model="item.resultDesc" :placeholder="'请输入'+item.tempItemNa"
                          v-if="(item.typeFlag == '3' && !IsEmpty(item.typeCode))"
                          :rows='IsEmpty(item.textHeight)?item.textHeight:1'
                          :class='item.tempItemNo'
                          @focus="showTypeCodeDrawer(item)"
                          :disabled="resultIsDisabled()"></vxe-textarea>

            <vxe-textarea v-if="!(item.typeFlag == '3' && !IsEmpty(item.typeCode))"
                          v-model="item.resultDesc" :placeholder="'请输入'+item.tempItemNa"
                          :rows='IsEmpty(item.textHeight)?item.textHeight:1'
                          @focusout="onSubmit(item)" :disabled="resultIsDisabled()"></vxe-textarea>

          </el-form-item>
        </el-col>
      <el-col :span="24" class='imgArea' v-if="item.typeFlag == '4'">
        <el-descriptions :size='size'>
          <el-descriptions-item :label="item.tempItemNa+'：'">
            <p>
              <img :src='apiPath+item.imageUrl' v-for='(item,index) in rptImages'>
            </p>
          </el-descriptions-item>
        </el-descriptions>
      </el-col>
      </el-row>

    </el-main>
    <FormEditDrawer ref="diagnosTempDrawerRef" @sltDiagnosTempInfo="onSubmit">
    </FormEditDrawer>
  </div>
</template>

<script lang="ts" setup>
// 引入组件区 ==========
import { ref, reactive, watch, getCurrentInstance, PropType, onMounted, inject, defineEmits,defineExpose } from 'vue';
import { IsEmpty, formatDateTime } from '@/utils/common'; //导入工具函数
import { VxeMessage, VxeNotifyType } from '@/utils/CommonMessage';
import { funBdsJsResult } from '@/utils/Expression'; //函数公式
import FormEditDrawer from '../img_txt_rpt/FormEditDrawer.vue';
import { AppStore } from '@/stores/store';
import { FormInstance,ComponentSize } from 'element-plus';
let labMainInfoObj: any = reactive({}); /**检验记录当前选中对象 */
// 全局变量定义区 ==========
let { proxy }: any = getCurrentInstance();
const apiPath = ref((window as any).apiUrl.VITE_SERVER_URL);
let store = AppStore();
let orgId = ref(null);
let iloading = ref(false);

//fixme:需要考虑这里还要不要留
const rptImages = inject('rptImages');
const size = ref<ComponentSize>('large');

const fields = reactive([]);
let templateId = ref(null);

defineExpose({
  onSubmit,
  initPage,
  reviewValidateVsCountIsEqual,
  findTemplateItemListByTempId
});

const proxyEmit = defineEmits(['updateTestList']);
// 父组件传值
let props = defineProps({
  currentRow: { type: Object as PropType<any>, required: true }
}) as any;

onMounted(() => {
  orgId = store.userInfo.orgId;
});

// 方法定义区 =============
// 保存信息
function onSubmit(item: any) {
  if (!IsEmpty(item.resultDesc)) {
    item.rptGroup = labMainInfoObj.rptGroup;
    item.instrCode = labMainInfoObj.rptGroup;
    item.instrNa = labMainInfoObj.rptGroupNa;
    item.rptGroupName = labMainInfoObj.rptGroupNa;
    item.isNeedToReport = 1;
    item.itemNo = item.tempItemNo;
    item.itemNa = item.tempItemNa;
    item.sampleNo = labMainInfoObj.sampleNo;
    item.sampleda = labMainInfoObj.sampleda;
    item.rptResult = item.resultDesc?.slice(0, 255); //截取前249个字符
    item.reqNo = labMainInfoObj.reqNo;
    item.maininfoId = labMainInfoObj.id;
    submitForm(item);
  }
}
// 保存
const submitForm = (item: any) => {
  const url = '/lab/rptResult/saveImgTxtRptValue';
  proxy.$http
    //注意此处的数据条数
    .post(url, item)
    .then((res: any) => {
      if (res.status.code === 1) {
        if (IsEmpty(labMainInfoObj.id)) {
          labMainInfoObj.id=res.data.maininfoId;
          proxyEmit('updateTestList', { isSelCurRow: true, isRefreshRptResult: false, mainInfoId: res.data.maininfoId });
        }
      } else {
        VxeMessage(res.status.msg, VxeNotifyType.error);
      }
    })
    .catch((err: any) => {
      console.log(err, 'submitFormerror');
    });
};
function initPage(mainInfoRow: any) {
  Object.assign(labMainInfoObj, mainInfoRow);
  if(IsEmpty(labMainInfoObj.id)){
      if(IsEmpty(templateId.value)){
        findTemplateItemList();
      }else{
        findTemplateItemListByTempId(templateId.value);
      }
    }else{
      findTemplateItemListByTempId(templateId.value);
    }
}
// 获取默认模板表单
function findTemplateItemList() {
  iloading.value = true;
  fields.length = 0; //放入数据前清空数据
  const url = '/lab/rptResult/findRptResultItemByTemp';
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        sampleDaStr: formatDateTime(labMainInfoObj.sampleda, 'yyyy-MM-dd'),
        rptGroup: labMainInfoObj.rptGroup,
        sampleNo: labMainInfoObj.sampleNo,
        reqNo: labMainInfoObj.reqNo,
        mainInfoId: labMainInfoObj.id
      }
    })
    .then((res: any) => {
      fields.length = 0; //放入数据前清空数据
      fields.push(...res.data);
      iloading.value = false;
    })
    .catch((err: any) => {
      iloading.value = false;
      VxeMessage('获取图报告单项目异常', VxeNotifyType.error);
    });
}
// 获取指定模板表单
function findTemplateItemListByTempId(tempId) {
  templateId.value = tempId;
  iloading.value = true;
  fields.length = 0; //放入数据前清空数据
  // 获取指定模板表单
  let url = '/lab/rptResult/findRptResultItemByTempId';
  // 若maininfo存在则获取更新模板表单
  if(!IsEmpty(labMainInfoObj.id)){
      url = '/lab/rptResult/upDataRptResultByTempId';
  }
  proxy.$http
    //注意此处的数据条数
    .get(url, {
      params: {
        templateId: templateId.value,
        // sampleda: formatDateTime(labMainInfoObj.sampleda, 'yyyy-MM-dd 00:00:00'),
        sampleDaStr: formatDateTime(labMainInfoObj.sampleda, 'yyyy-MM-dd'),
        rptGroup: labMainInfoObj.rptGroup,
        sampleNo: labMainInfoObj.sampleNo,
        reqNo: labMainInfoObj.reqNo,
        mainInfoId: labMainInfoObj.id
      }
    })
    .then((res: any) => {
      fields.length = 0; //放入数据前清空数据
      fields.push(...res.data);
      iloading.value = false;
    })
    .catch((err: any) => {
      iloading.value = false;
      VxeMessage('获取图报告单项目异常', VxeNotifyType.error);
    });
}

function showTypeCodeDrawer(item) {
  showDiagnosTempDrawer(item);
}
function colseTypeCodeDrawer(){
  diagnosTempDrawerRef.value.colseTypeCodeDrawer();
}
let diagnosTempDrawerRef = ref(null);

function showDiagnosTempDrawer(item) {
  diagnosTempDrawerRef.value.initPage(item);
}

function resultIsDisabled() {
  if (!IsEmpty(labMainInfoObj) && labMainInfoObj.isChk == '2') {
    return true;
  } else {
    return false;
  }
}
/**审核验证，项目对照，检验结果数量是否相等 */
async function reviewValidateVsCountIsEqual() {
  let res = false;
  try {
    res = false;
  } catch (error) {}
  return res;
}

</script>

<style lang="scss" scoped>
#imgtxtrpt {
  height: calc(80vh - 10px);
  padding: 21px;
  overflow: auto;
  // 中等尺寸时 配置扩展属性按钮
  .el-button--default {
    padding: 5px 11px;
  }
  table {
    border-collapse: collapse; /* 确保表格边框合并为单一边框 */
  }

  table,
  th,
  td {
    border: 1px solid black; /* 设置表格、表头、单元格的边框 */
    text-align: center;
  }
  .imgArea {
    margin-top: 4px;
    font-size: 14px;
    p {
      text-align: center;
      img {
        width: 40%;
        padding: 5px;
      }
    }
    .textArea {
      min-height: 30px;
    }
  }

  .font14 {
    font-size: 14px;
  }
}
#imgtxtrpt::-webkit-scrollbar {
  display: none;
}
</style>

